﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SeriesManager.Model;

namespace SeriesManager.Test.Model
{
    [TestClass]
    public class EpisodeTest
    {
        #region Constructor tests

        [TestMethod]
        public void EpisodeConstructorTestSuccessfullEmpty()
        {
            Episode target = new Episode();

            Assert.AreEqual(string.Empty, target.Name);
            Assert.AreEqual(0, target.Number);
            Assert.AreEqual(string.Empty, target.EpisodePath);
            Assert.IsNull(target.Season);
        }

        [TestMethod]
        public void EpisodeConstructorTestSuccessfullFilePath()
        {
            string path = @"C:\Some\Folder\With\someFile.mkv";

            Episode target = new Episode(path);

            Assert.AreEqual(path, target.EpisodePath);
            Assert.AreEqual(string.Empty, target.Name);
            Assert.AreEqual(0, target.Number);
            Assert.IsNull(target.Season);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void EpisodeConstructorTestFailedNoPath()
        {
            Episode target = new Episode(string.Empty);
        }

        #endregion

        #region Update tests

        [TestMethod]
        public void UpdateTestSuccessfull()
        {
            string xmlContent =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Data><Episode><id>398671</id><Combined_episodenumber>1.0</Combined_episodenumber><Combined_season>1</Combined_season><DVD_chapter></DVD_chapter><DVD_discid></DVD_discid><DVD_episodenumber>1.0</DVD_episodenumber><DVD_season>1</DVD_season><Director>Rob Bowman</Director><EpImgFlag>2</EpImgFlag><EpisodeName>Blumen für Dein Grab</EpisodeName><EpisodeNumber>1</EpisodeNumber><FirstAired>2009-03-09</FirstAired><GuestStars>Stephen J. Cannell|James Patterson|Dan Castellaneta|Keir Dullea|Colby French|Brian Avers|Monet Mazur</GuestStars><IMDB_ID>tt1303973</IMDB_ID><Language>de</Language><Overview>Der Anwalt Marvin Fisk und die Sozialarbeiterin Alison Tisdale werden ermordet aufgefunden. Detective Kate Beckett fällt auf, dass es eine Verbindung zwischen den Morden gibt: Es handelt sich um Verbrechen aus Romanen des Kriminalautors Rick Castle. Dieser fühlt sich durch den Nachahmer geschmeichelt, und willigt ein, der Polizei bei den Ermittlungen zu helfen. Beckett ist davon alles andere als begeistert, doch schon bald erweist sich Castles Hilfe als unentbehrlich ...</Overview><ProductionCode>101</ProductionCode><Rating>7.9</Rating><RatingCount>125</RatingCount><SeasonNumber>1</SeasonNumber><Writer>Andrew W. Marlowe</Writer><absolute_number>1</absolute_number><filename>episodes/83462/398671.jpg</filename><lastupdated>1376283694</lastupdated><seasonid>36354</seasonid><seriesid>83462</seriesid><thumb_added></thumb_added><thumb_height>225</thumb_height><thumb_width>400</thumb_width><tms_export>1</tms_export><tms_review_blurry>0</tms_review_blurry><tms_review_by></tms_review_by><tms_review_dark>0</tms_review_dark><tms_review_date></tms_review_date>  <tms_review_logo>0</tms_review_logo><tms_review_other>0</tms_review_other><tms_review_unsure>0</tms_review_unsure></Episode></Data>";
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(xmlContent);

            System.Xml.XmlNode dataNode = doc.ChildNodes[1];
            System.Xml.XmlNode episodeNode = dataNode.ChildNodes[0];
            TVDB.Model.Episode toUpdate = new TVDB.Model.Episode();
            toUpdate.Deserialize(episodeNode);

            Episode target = new Episode();
            target.UpdateDetails(toUpdate);

            Assert.AreEqual(1, target.Number);
            Assert.AreEqual("Blumen für Dein Grab", target.Name);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void UpdateTestFailedNoEpisodeProvided()
        {
            Episode target = new Episode();
            target.UpdateDetails(null);
        }

        #endregion

        #region ToString tests

        [TestMethod]
        public void ToStringTestSuccessfullSeasonNumberAndEpisodeNumberSmaller10()
        {
            Series series = new Series()
            {
                Name = "Bones",
            };

            Season season1 = new Season(series, 8);

            Episode target = new Episode();

            int episodeNumber = 5;
            string episodeName = "Schuss in den Wind";

            target.Season = season1;
            target.Name = episodeName;
            target.Number = episodeNumber;

            season1.AddEpisode(target);

            string expected = "Bones S08E05 - Schuss in den Wind";
            string actual = target.ToString();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ToStringTestSuccessfullSeasonNumberAndEpisodeNumberGreater10()
        {
            Series series = new Series()
            {
                Name = "Bones",
            };

            Season season1 = new Season(series, 11);

            Episode target = new Episode();

            int episodeNumber = 24;
            string episodeName = "Schuss in den Wind";

            target.Season = season1;
            target.Name = episodeName;
            target.Number = episodeNumber;

            season1.AddEpisode(target);

            string expected = "Bones S11E24 - Schuss in den Wind";
            string actual = target.ToString();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ToStringTestFailedMissingInformationNoSeasoneNumber()
        {
            Series series = new Series()
            {
                Name = "Bones",
            };

            Season season1 = new Season(series, 8);

            Episode target = new Episode();

            string episodeName = "Schuss in den Wind";

            target.Name = episodeName;


            string actual = target.ToString();

            Assert.AreEqual(string.Empty, actual);
        }

        #endregion
    }
}
